/*
 * OrderRestController.java
 * Copyright (c) 2014, 2019, Kiri. 
 * All rights reserved.
 */
package com.onesalt.web.api;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.onesalt.common.ExceptionConstants;
import com.onesalt.domain.Dinner;
import com.onesalt.domain.Kitchen;
import com.onesalt.domain.Order;
import com.onesalt.domain.User;
import com.onesalt.service.DinnerService;
import com.onesalt.service.KitchenService;
import com.onesalt.service.OrderService;
import com.onesalt.service.PingPPService;
import com.onesalt.utils.JSONUtil;
import com.onesalt.web.api.response.OrderResponse;
import com.onesalt.web.api.response.PrepayResponse;
import com.pingplusplus.model.Charge;

/**
 * 订单
 *
 * @author kiri
 * @date 2015年5月17日
 * @version 1.0
 */
@RequestMapping("/api/orders")
@RestController
public class OrderRestController extends AbstractRestController {

	@Autowired
	private PingPPService pingPPService;

	@Autowired
	private OrderService orderService;

	@Autowired
	private DinnerService dinnerService;
	
	@Autowired
	private KitchenService kitchenService;

	public PingPPService getPingPPService() {
		return pingPPService;
	}

	public OrderService getOrderService() {
		return orderService;
	}

	public DinnerService getDinnerService() {
		return dinnerService;
	}

	@RequestMapping(value = "/prepay", method = RequestMethod.POST)
	public PrepayResponse prepay(@RequestParam Long dinnerId, @RequestParam Integer amount, @RequestParam String channel) {
		Dinner dinner = getDinnerService().getDinnerRepository().findOne(dinnerId);
		if (dinner == null) {
			throw ExceptionConstants.DINNER_NOT_EXISTS_EXCEPTION;
		}

		Kitchen kitchen = getKitchenService().getKitchenRepository().findOne(dinner.getKitchenId());
		if (kitchen == null) {
			throw ExceptionConstants.KITCHEN_NOT_EXISTS_EXCEPTION;
		}
		
		String pingppOrderNo = getOrderService().generateSn(dinner);
		String subject = kitchen.getName();
		String body = subject;
		Charge charge = getPingPPService().createCharge(pingppOrderNo, amount, subject, body, channel);
		if (charge == null) {
			throw ExceptionConstants.ORDER_PAY_FAILED_EXCEPTION;
		}

		PrepayResponse response = new PrepayResponse();
		response.setOrderNo(pingppOrderNo);
		response.setCharge(JSONUtil.toJsonString(charge));
		return response;
	}

	@RequestMapping(value = "/addPaidOrder", method = RequestMethod.POST)
	public OrderResponse addPaidOrder(@RequestParam Long dinnerId, @RequestParam Integer amount, @RequestParam Long dinerId,
			@RequestParam Integer peopleNumber, @RequestParam String notes, @RequestParam String charge) {
		Dinner dinner = getDinnerService().getDinnerRepository().findOne(dinerId);
		if (dinner == null) {
			throw ExceptionConstants.DINNER_NOT_EXISTS_EXCEPTION;
		}

		if (dinner.getDeadline() != null && new Date().after(dinner.getDeadline())) {
			throw ExceptionConstants.ORDER_DINNER_SOLD_OUT_EXCEPTION;
		}

		if (dinner.getStatus() != null && dinner.getStatus().equals(Dinner.STATUS_CANCELLED)) {
			throw ExceptionConstants.ORDER_DINNER_CANCELLED_EXCEPTION;
		}

		if (dinner.getType() != null && dinner.getType().equals(Dinner.TYPE_PRIVATE)) {
			if (dinner.getPeopleNumber() != null && dinner.getPeopleNumber() > 0) {
				throw ExceptionConstants.ORDER_DINNER_SOLD_OUT_EXCEPTION;
			}
		}

		if (dinner.getPeopleNumber() != null && dinner.getMaxPeopleNumber() != null && peopleNumber != null
				&& (dinner.getPeopleNumber() + peopleNumber) > dinner.getMaxPeopleNumber()) {
			throw ExceptionConstants.ORDER_DINNER_SOLD_OUT_EXCEPTION;
		}

		User diner = getUserService().getUserRepository().findOne(dinerId);
		if (diner == null) {
			throw ExceptionConstants.DINER_NOT_EXISTS_EXCEPTION;
		}
		
		Kitchen kitchen = getKitchenService().getKitchenRepository().findOne(dinner.getKitchenId());
		if (kitchen == null) {
			throw ExceptionConstants.KITCHEN_NOT_EXISTS_EXCEPTION;
		}

		User cook = getUserService().getUserRepository().findOne(kitchen.getCookId());
		Order o = getOrderService().createPaidOrder(dinner, diner, cook, amount, dinerId, peopleNumber, notes, charge);
		OrderResponse response = new OrderResponse();
		response.setOrder(o);
		return response;
	}

	public KitchenService getKitchenService() {
		return kitchenService;
	}
}
